<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item label>
          <!-- <el-select v-model="filters._area" placeholder="区域编号" @change="areaChange" clearable>
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>-->
          <area-c v-bind:area="filters._area" ></area-c>
        </el-form-item>
        <el-form-item label>
          <el-select v-model="filters._dorm" placeholder="宿舍楼编号" clearable>
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.caretaker" placeholder="宿管员"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" @click="newFormVisible = true">新增</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>

    <!-- 操作栏位 -->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button type="success" @click="newFormVisible = true" size="small">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="outExcel" size="small">导出EXCEL</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 数据展示 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="Loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="affiliationName" fixed min-width="150"></el-table-column>
      <el-table-column align="center" label="区域编号" prop="affiliationNumber" fixed min-width="100"></el-table-column>
      <el-table-column align="center" label="宿舍楼编号" prop="dormNumber" min-width="150"></el-table-column>
      <el-table-column align="center" label="宿舍楼名称" prop="dormName" min-width="100"></el-table-column>
      <el-table-column align="center" label="楼栋号" prop="number" min-width="50"></el-table-column>
      <el-table-column align="center" label="创建人员" prop="createman" min-width="100"></el-table-column>
      <el-table-column align="center" label="创建时间" prop="createtime" min-width="100"></el-table-column>
      <el-table-column align="center" label="楼层" prop="layers" min-width="50"></el-table-column>
      <el-table-column align="center" label="宿管员" prop="caretaker" min-width="60"></el-table-column>
      <el-table-column align="center" label="入住人数" prop="person" min-width="80"></el-table-column>
      <el-table-column align="center" label="备注" prop="remark" min-width="50"></el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!-- 新增 -->
    <el-dialog title="新增" :visible.sync="newFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="newForm" :model="newDorm" :rules="rule">
        <el-form-item label="所属区域" prop="area">
          <el-select v-model="newDorm.area" placeholder="请选择" clearable>
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍楼名称" prop="dormName">
          <el-input v-model="newDorm.dormName"></el-input>
        </el-form-item>
        <el-form-item label="楼层" prop="layers">
          <el-input v-model.number="newDorm.layers"></el-input>
        </el-form-item>
        <el-form-item label="宿管员">
          <el-input v-model="newDorm.caretaker"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="newDorm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="newFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="editForm" :model="alterDorm" :rules="rule">
        <el-form-item label="宿舍楼名称" prop="dormName">
          <el-input v-model="alterDorm.dormName"></el-input>
        </el-form-item>
        <el-form-item label="宿管员">
          <el-input v-model="alterDorm.caretaker"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="alterDorm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit('editForm')" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { getArea } from "../../api/area.js";
import {
  getDorm,
  searchDorm,
  newDorm,
  alterDorm,
  deleteDorm
} from "../../api/dorm.js";
import AreaC from "../../components/AreaC";
// import { valid } from "semver";
export default {
  components: { AreaC },
  data() {
    return {
      filters: {
        caretaker: "",
        _area: "",
        _dorm: "",
        page: 1
      },
      area: [
        {
          value: "",
          label: ""
        }
      ],
      dorm: [
        {
          value: "",
          label: ""
        }
      ],
      tableData: [
        {
          affiliationName: null,
          affiliationNumber: null,
          dormNumber: null,
          dormName: null,
          createman: null,
          createtime: null,
          layers: null,
          caretaker: null,
          person: null,
          remark: null,
          number: null
        }
      ],
      newDorm: {
        dormName: null,
        layers: null,
        caretaker: null,
        area: null,
        createman: null,
        remark: null
      },
      alterDorm: {
        dormName: null,
        caretaker: null,
        remark: null
      },
      rule: {
        area: [
          { required: true, message: "请选择宿舍楼区域", trigger: "change" }
        ],
        dormName: [
          { required: true, message: "请输入宿舍楼名称", trigger: "blur" }
        ],
        layers: [
          { required: true, message: "请输入宿舍楼层数", trigger: "blur" },
          { type: "number", message: "值必须为数字值" }
        ]
      },
      Loading: false,
      newLoading: false,
      editLoading: false,
      newFormVisible: false,
      alterFormVisible: false,
      currentPage: 1,
      totalPage: 1
    };
  },
  methods: {
    // 获取区域
    getArea() {
      getArea().then(res => {
        if (res) {
          this.area = res.msg;
        }
      });
    },
    // 获取数据
    getData() {
      this.Loading = true;
      searchDorm(this.filters).then(res => {
        this.Loading = false;
        if (res) {
          this.tableData = res.msg;
          this.totalPage = res.page;
        } else {
          this.tableData = [];
          this.totalPage = 0;
          this.currentPage = 0;
        }
      });
    },
    // 查询
    search() {
      this.filters.page = 1;
      this.getData();
    },
    // 分页
    handleCurrentChange(val) {
      this.filters.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    // 点击编辑
    handleEdit($index, row) {
      this.alterFormVisible = true;
      this.alterDorm = Object.assign({}, row);
    },
    // 编辑
    editSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.editLoading = true;
          alterDorm(this.alterDorm).then(res => {
            this.editLoading = false;
            if (res) {
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: action => {
                  this.getData();
                  this.alterFormVisible = false;
                }
              });
            }
          });
        }
      });
    },
    // 删除
    handleDel(row) {
      let affiliationNumber = row.affiliationNumber;
      let dormNumber = row.dormNumber;
      // 提示是否删除
      this.$confirm("此操作将永久删除该区域下所有内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 进行删除操作
          this.Loading = true;
          deleteDorm(affiliationNumber, dormNumber).then(res => {
            this.Loading = false;
            if (res) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 新建
    newSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.newLoading = true;
          this.newDorm.createman = JSON.parse(
            sessionStorage.getItem("user")
          ).user;
          newDorm(this.newDorm).then(res => {
            this.newLoading = false;
            if (res) {
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: action => {
                  this.getData();
                  this.newFormVisible = false;
                }
              });
            }
          });
        }
      });
    },
    // 选完区域后触发宿舍楼
    areaChange() {
      // 搜索宿舍楼编号
      // console.log(this.filters._area);
      if (this.filters._area === "") {
        return true;
      }
      getDorm(this.filters._area).then(res => {
        if (res) {
          this.filters._dorm = null;
          this.dorm = res.msg;
        } else {
          this.dorm = [
            {
              value: "",
              label: ""
            }
          ];
          this.filters._dorm = "";
        }
      });
    },
    selsChange: function(sels) {
      this.sels = sels;
    },
    outExcel() {
      window.location.href =
        "http://192.168.10.225:80/dormitoryA/building/Building/dormInfoExcelOut";
    }
  },
  mounted() {
    this.getArea();
    this.search();
  }
};
</script>
<style>
.el-dropdown {
  vertical-align: top;
}
.el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
